#ifndef MSA9020_H
#define MSA9020_H

#include <QObject>
#include <QQueue>
#include <QHash>
#include <QSettings>  //读写ini文件
#include <QDebug>
#include <QtMath>
#include <QTime>
#include <QLabel>

#define CARD_INFO_ADDR    101  // 通道卡信息地址
#define UNIT_MEASUR_CHA   13   // A通道测量单位地址
#define UNIT_MEASUR_CHB   57   // B通道测量单位地址
#define MAX_CARDS_COUNT   20   // 支持通道卡的最大个数

#define ELE     0
#define ELEm    1
#define PPM     2
#define VOL     3
#define GM3     4


// 故障警告
#define NF      0   //no failure
#define F1      1   // 电源线路故障
#define F2      2   // 输入信号故障
#define F3      3   // 存储器（EEPROM）程序出错
#define F4      4   // 操作功能码时系统掉电
#define F5      5   // 模拟信号相关的A/D转换器故障
#define F6      6   // 存储器（RAM)故障
#define F7      7   // 只读存储器（EPROM）产生错误
#define F8      8   // 控制器内随机存储器数据设置发生错误


#define NOT_DRIFT   0   // 不等于0，就是漂移了
#define EEEE        1   // 超量程
#define CAUTION     1   // 小心
#define WARNING     1   // 预报
#define ALARM       1   // 高报

// 错误警告
#define E01     1     // 零信号和满量程之间的信号差<2mV
#define E02     2     // 零信号和满量程之间的信号差>=200mV
#define E03     3     // 零信号和满量程之间的信号差 3mV>n>2mV
#define E04     4     // 输入信号高于 +300mV

#define CHA     1
#define CHB     2


#pragma pack(push,2)  // 字节对齐

typedef union CardInfo {
    struct {
        qint16 v_real_cha = 0;
        quint16 decimal_cha = 0;
        qint16 v_real_chb = 0;
        quint16 decimal_chb = 0;

        quint16 state_fail_cha = 0;
        quint16 state_drift_cha = 0;
        quint16 state_eeee_cha = 0;
        quint16 state_c_cha = 0;
        quint16 state_w_cha = 0;
        quint16 state_a_cha = 0;
        quint16 e1_cha = 0;
        quint16 e2_cha = 0;
        quint16 e3_cha = 0;
        quint16 e4_cha = 0;

        quint16 state_fail_chb = 0;
        quint16 state_drift_chb = 0;
        quint16 state_eeee_chb = 0;
        quint16 state_c_chb = 0;
        quint16 state_w_chb = 0;
        quint16 state_a_chb = 0;
        quint16 e1_chb = 0;
        quint16 e2_chb = 0;
        quint16 e3_chb = 0;
        quint16 e4_chb = 0;
    };
    quint16 data[24];
} CARD_INFO;

#pragma pack(pop)   // 字节对齐END

typedef struct Cards {
    CARD_INFO ci;
    int     index = 0, overtimeCount = 0;
    QString aname;              // A通道名称
    QString bname;              // B通道名称
    quint16 unit_measur_cha = 5;    // A通道测量单位
    quint16 unit_measur_chb = 5;    // B通道测量单位

    QLabel *labelModbusAddr;
    QLabel *labelAddrA;
    QLabel *labelAddrB;
    QLabel *labelCHA_value;
    QLabel *labelCHB_value;
    QLabel *labelAc;
    QLabel *labelBc;
    QLabel *labelAf;
    QLabel *labelBf;
    QLabel *labelAl;
    QLabel *labelBl;
    QLabel *labelAh;
    QLabel *labelBh;

    bool fa,fb,da,db,ea,eb,ca,cb,wa,wb,aa,ab,e1a,e1b,e2a,e2b,e3a,e3b,e4a,e4b;
} CARDS;


class Msa9020 : public QObject
{
    Q_OBJECT
public:
    explicit Msa9020(QObject *parent = 0);

    void initMsa(QString iniFile, QWidget *p);
    void setUnitMeasurCha(quint8 key, quint16 value);
    void setUnitMeasurChb(quint8 key, quint16 value);
    void setNameCha(quint8 key, QString name);
    void setNameChb(quint8 key, QString name);
    bool contains(quint8 key);
    void setAlarmState(quint8 key);

    QString getValueStrCha(quint8 key);
    QString getValueStrChb(quint8 key);
    float getValueCha(quint8 key);
    float getValueChb(quint8 key);
    QString getUnitMeasurCha(quint8 key);
    QString getUnitMeasurChb(quint8 key);
    int   getChIndex(quint8 key);

    QString getStateFailStrCha(quint8 key);
    QString getStateFailStrChb(quint8 key);
    QString getStateFailInfoCha(quint8 key);
    QString getStateFailInfoChb(quint8 key);
    int  getStateFailCha(quint8 key);
    int  getStateFailChb(quint8 key);

    int  getStateDriftCha(quint8 key);
    int  getStateDriftChb(quint8 key);
    int  getStateEEEECha(quint8 key);
    int  getStateEEEEChb(quint8 key);
    int  getCautionCha(quint8 key);
    int  getCautionChb(quint8 key);
    int  getWarningCha(quint8 key);
    int  getWarningChb(quint8 key);
    int  getAlarmCha(quint8 key);
    int  getAlarmChb(quint8 key);
    int  getErrorE1Cha(quint8 key);
    int  getErrorE1Chb(quint8 key);
    int  getErrorE2Cha(quint8 key);
    int  getErrorE2Chb(quint8 key);
    int  getErrorE3Cha(quint8 key);
    int  getErrorE3Chb(quint8 key);
    int  getErrorE4Cha(quint8 key);
    int  getErrorE4Chb(quint8 key);

    void refreshDisplay(quint8 key); // 更新界面数据显示
    void displayOvertime(quint8 key);// 显示超时状态

    //int ChTag;
    QMap<quint8, CARDS> cards;
    QString SFC[9][2] = {
        {"NF", tr("no failure")},
        {"F1", tr("F1电源线路故障")},
        {"F2", tr("F2输入信号故障")},
        {"F3", tr("F3存储器（EEPROM）程序出错")},
        {"F4", tr("F4操作功能码时系统掉电")},
        {"F5", tr("F5模拟信号相关的A/D转换器故障")},
        {"F6", tr("F6存储器（RAM)故障")},
        {"F7", tr("F7只读存储器（EPROM）产生错误")},
        {"F8", tr("F8控制器内随机存储器数据设置发生错误")}
    };


private:
    QString UMC[6] = {"ELE", "ELEm", "PPM", "%", "g/m3", ""};

    int otcount = 10;

signals:

public slots:
};


#endif // MSA9020_H
